﻿/* QF classbuilder for Enlib */
using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging;

#region SYS_Privilege实体类
public class SYS_Privilege
{
	private string m_PrivilegeID;
	private string m_Name;
	private string m_Description;
	private bool m_Enabled;
	private DateTime m_LastModifiedTime;
	private string m_LastModifiedByID;
	
	public SYS_Privilege ()
	{
		m_PrivilegeID="";
		m_Name="";
		m_Description="";
		m_Enabled=false;
		m_LastModifiedTime=DateTime.Now;
		m_LastModifiedByID="";
	}
	
	public string PrivilegeID
	{
		get{return m_PrivilegeID;}
		set{m_PrivilegeID=value;}
	}
	public string Name
	{
		get{return m_Name;}
		set{m_Name=value;}
	}
	public string Description
	{
		get{return m_Description;}
		set{m_Description=value;}
	}
	public bool Enabled
	{
		get{return m_Enabled;}
		set{m_Enabled=value;}
	}
	public DateTime LastModifiedTime
	{
		get{return m_LastModifiedTime;}
		set{m_LastModifiedTime=value;}
	}
	public string LastModifiedByID
	{
		get{return m_LastModifiedByID;}
		set{m_LastModifiedByID=value;}
	}
}
#endregion

#region SYS_Privilege管理类
public partial class LogicLayer
{
	public bool AddSYS_Privilege(SYS_Privilege theSYS_Privilege)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "insert into SYS_Privilege (PrivilegeID,Name,Description,Enabled,LastModifiedTime,LastModifiedByID) values (@PrivilegeID,@Name,@Description,@Enabled,@LastModifiedTime,@LastModifiedByID)";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			db.AddInParameter(cmd, "@PrivilegeID", DbType.String, theSYS_Privilege.PrivilegeID);
			db.AddInParameter(cmd, "@Name", DbType.String, theSYS_Privilege.Name);
			db.AddInParameter(cmd, "@Description", DbType.String, theSYS_Privilege.Description);
			db.AddInParameter(cmd, "@Enabled", DbType.Boolean, theSYS_Privilege.Enabled);
			db.AddInParameter(cmd, "@LastModifiedTime", DbType.DateTime, theSYS_Privilege.LastModifiedTime);
			db.AddInParameter(cmd, "@LastModifiedByID", DbType.String, theSYS_Privilege.LastModifiedByID);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("AddSYS_Privilege:" + ex.Message );
			return false;
		}
	}
	
	public SYS_Privilege GetSYS_PrivilegeFromDataReader(IDataReader dataReader)
	{
		try
		{
			SYS_Privilege result = new SYS_Privilege();
			
			if (dataReader["PrivilegeID"] != null && dataReader["PrivilegeID"] != System.DBNull.Value )
				result.PrivilegeID = (string)dataReader["PrivilegeID"];
			if (dataReader["Name"] != null && dataReader["Name"] != System.DBNull.Value )
				result.Name = (string)dataReader["Name"];
			if (dataReader["Description"] != null && dataReader["Description"] != System.DBNull.Value )
				result.Description = (string)dataReader["Description"];
			if (dataReader["Enabled"] != null && dataReader["Enabled"] != System.DBNull.Value )
				result.Enabled = (bool)dataReader["Enabled"];
			if (dataReader["LastModifiedTime"] != null && dataReader["LastModifiedTime"] != System.DBNull.Value )
				result.LastModifiedTime = (DateTime)dataReader["LastModifiedTime"];
			if (dataReader["LastModifiedByID"] != null && dataReader["LastModifiedByID"] != System.DBNull.Value )
				result.LastModifiedByID = (string)dataReader["LastModifiedByID"];
			
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("GetReaderSYS_Privilege:" + ex.Message);
			return null;
		}
	}
	
	public SYS_Privilege GetSYS_Privilege(string PrivilegeID)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "select * From SYS_Privilege where PrivilegeID=@PrivilegeID";
			
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			db.AddInParameter(cmd, "@PrivilegeID", DbType.String, PrivilegeID);
			
			SYS_Privilege result = null;
			using (IDataReader dataReader = db.ExecuteReader(cmd))
			{
				if (dataReader.Read())
				{
					result = GetSYS_PrivilegeFromDataReader( dataReader );
				}
			}
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("GetSYS_Privilege:" + ex.Message);
			return null;
		}
	}
	
	public bool DelSYS_Privilege(string PrivilegeID)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "delete From SYS_Privilege where PrivilegeID=@PrivilegeID";
			
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			db.AddInParameter(cmd, "@PrivilegeID", DbType.String, PrivilegeID);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("DelSYS_Privilege:" + ex.Message);
			return false;
		}
	}
	
	public bool UpdateSYS_Privilege(SYS_Privilege theSYS_Privilege)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "update SYS_Privilege set Name=@Name,Description=@Description,Enabled=@Enabled,LastModifiedTime=@LastModifiedTime,LastModifiedByID=@LastModifiedByID where PrivilegeID=@PrivilegeID";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			db.AddInParameter(cmd, "@Name", DbType.String, theSYS_Privilege.Name);
			db.AddInParameter(cmd, "@Description", DbType.String, theSYS_Privilege.Description);
			db.AddInParameter(cmd, "@Enabled", DbType.Boolean, theSYS_Privilege.Enabled);
			db.AddInParameter(cmd, "@LastModifiedTime", DbType.DateTime, theSYS_Privilege.LastModifiedTime);
			db.AddInParameter(cmd, "@LastModifiedByID", DbType.String, theSYS_Privilege.LastModifiedByID);
			db.AddInParameter(cmd, "@PrivilegeID", DbType.String, theSYS_Privilege.PrivilegeID);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("UpdateSYS_Privilege:" + ex.Message);
			return false;
		}
	}
	
	public List<SYS_Privilege> ListSYS_Privilege()
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "select * from SYS_Privilege";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			 List<SYS_Privilege> result = new  List<SYS_Privilege>();
			using (IDataReader dataReader = db.ExecuteReader(cmd))
			{
				while (dataReader.Read())
				{
					SYS_Privilege obj = GetSYS_PrivilegeFromDataReader( dataReader );
					if(obj != null)
						result.Add(obj);
				}
			}
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("ListSYS_Privilege:" + ex.Message);
			return null;
		}
	}
	
}
#endregion